﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;
using DB.From;
using Models;
using Microsoft.IdentityModel.Logging;
using WebApplication1.Utils;
using Newtonsoft.Json;
using WebApplication1.Services;

namespace WebApplication1.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MapController : ControllerBase
    {
        public AlarmService AlarmService { get; set; }

        public MapController()
        {
            AlarmService = new AlarmService();
        }
        /// <summary>
        /// 测试数据库是否连接成功
        /// </summary>
        /// <param name="dataname"></param>
        /// <returns></returns>
        [HttpGet]
        public bool IsConnect(string dataname)
        {
            var db = new MysqlSugar(dataname);
            MyLogHelper.Debug($"尝试链接数据库{dataname}");
            return db.IsConnect();
        }

        /// <summary>
        /// 地钉映射
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public bool EarthNail(t_earthNail model,string dataname)
        {
            var db = new MysqlSugar(dataname);
            MyLogHelper.Debug($"尝试转发地钉[t_earthNail][{model.TerminalId}]数据，数据库[{dataname}]");

            db.UpdateTime(model.TerminalId);
            return db.MapData(model);
        }

        /// <summary>
        /// 接地箱映射
        /// </summary>
        /// <param name="model"></param>
        /// <param name="dataname"></param>
        /// <returns></returns>
        [HttpPost]
        public bool EarthBox(t_earthBox model,string dataname)
        {
            var db = new MysqlSugar(dataname);
            MyLogHelper.Debug($"尝试转发接地箱[t_earthBox][{model.TerminalId}]数据，数据库[{dataname}]");
            return db.MapData(model);
        }

        /// <summary>
        /// 电缆位移II映射
        /// </summary>
        /// <param name="model"></param>
        /// <param name="dataname"></param>
        /// <returns></returns>
        [HttpPost]
        public bool DistanceII(t_DistanceII model, string dataname)
        {
            var db = new MysqlSugar(dataname);
            MyLogHelper.Debug($"尝试转发电缆位移II[t_DistanceII][{model.TerminalId}]数据，数据库[{dataname}]");
            db.UpdateTime(model.TerminalId);

            return db.MapData(model);
        }

        /// <summary>
        /// 环流映射
        /// </summary>
        /// <param name="model"></param>
        /// <param name="dataname"></param>
        /// <returns></returns>
        [HttpPost]
        public bool FreeServicing(t_freeServicing model, string dataname)
        {
            var db = new MysqlSugar(dataname);
            MyLogHelper.Debug($"尝试转发电缆位移II[t_DistanceII][{model.TerminalId}]数据，数据库[{dataname}]");
            db.UpdateTime(model.TerminalId);

            return db.MapData(model);
        }

        /// <summary>
        /// 环压映射
        /// </summary>
        /// <param name="model"></param>
        /// <param name="dataname"></param>
        /// <returns></returns>
        [HttpPost]
        public bool FreeVoltServicing(t_freeVoltServicing model, string dataname)
        {
            var db = new MysqlSugar(dataname);
            db.UpdateTime(model.TerminalId);

            return db.MapData(model);
        }

        /// <summary>
        /// 外置局放映射
        /// </summary>
        /// <param name="model"></param>
        /// <param name="dataname"></param>
        /// <returns></returns>
        [HttpPost]
        public bool OutPartial(t_outPartial model, string dataname)
        {
            var db = new MysqlSugar(dataname);
            db.UpdateTime(model.TerminalId);

            return db.MapData(model);
        }

        /// <summary>
        /// 内置局放映射
        /// </summary>
        /// <param name="model"></param>
        /// <param name="dataname"></param>
        /// <returns></returns>
        [HttpPost]
        public bool Partial(t_Partial model, string dataname)
        {
            var db = new MysqlSugar(dataname);
            db.UpdateTime(model.TerminalId);

            return db.MapData(model);
        }


        /// <summary>
        /// 光纤测温映射
        /// </summary>
        /// <param name="model"></param>
        /// <param name="dataname"></param>
        /// <returns></returns>
        [HttpPost]
        public bool OpticalTemp(t_OpticalTemp model, string dataname)
        {
            var db = new MysqlSugar(dataname);
            MyLogHelper.Debug($"尝试转发光纤测温映射[t_OpticalTemp][{model.TerminalId}]数据，数据库[{dataname}]");
            db.UpdateTime(model.TerminalId);
            //AlarmService.CheckAlarm(model);
            return db.MapData(model);
        }

        /// <summary>
        /// 环境量映射映射
        /// </summary>
        /// <param name="model"></param>
        /// <param name="dataname"></param>
        /// <returns></returns>
        [HttpPost]
        public bool Environment(t_environment model, string dataname)
        {
            var db = new MysqlSugar(dataname);
            db.UpdateTime(model.TerminalId);

            return db.MapData(model);
        }

        /// <summary>
        /// 故障定位映射
        /// </summary>
        /// <param name="model"></param>
        /// <param name="dataname"></param>
        /// <returns></returns>
        [HttpPost]
        public bool ErrorLocation(t_errorLocation model, string dataname)
        {
            var db = new MysqlSugar(dataname);
            db.UpdateTime(model.TerminalId);

            return db.MapData(model);
        }
    }
}
